<?php
/**
 * @version		$Id: invoices.php 3 2011-12-16 20:19:15Z netix.mail@gmail.com $
 * @package		ALM Hoster
 * @desription	Web hosting and domain menager for resellers
 * @subpackage	Components
 * @copyright	Copyright (C) 2011 ALM Studio. All rights reserved.
 * @author		ALM Studio
 * @email		info@alm-studio.net
 * @link		http://alm-studio.net
 * @license		Commercial
 */

// No direct access to this file
defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.modellist');

class AlmHosterModelInvoices extends JModelList
{

	public function __construct($config = array())
	{
		if (empty($config['filter_fields'])) {
			$config['filter_fields'] = array(
				'InvoiceId', 'a.InvoiceId',
				'InvoiceOrder', 'a.InvoiceOrder', 'order',
				'InvoiceClient', 'a.InvoiceClient', 'client',
				'InvoiceDate', 'a.InvoiceDate',
				'InvoiceDue', 'a.InvoiceDue',
				'InvoiceProduct', 'a.InvoiceProduct', 'product',
				'InvoiceAmount', 'a.InvoiceAmount',
				'InvoiceStatus', 'a.InvoiceStatus',
			);
		}

		parent::__construct($config);
	}
	
	protected function populateState($ordering = null, $direction = null)
	{

		$app = JFactory::getApplication('site');
	
		
		parent::populateState('a.InvoiceId', 'desc');		
				
	}

	
	protected function getListQuery() 
	{
		$user	= JFactory::getUser();
		$db = JFactory::getDBO();
		$query = $db->getQuery(true);
		
		$query->select('ClientId');
		$query->from('#__almh_clients');
		$query->where('ClientUser =' .$user->get('id'));
		$db->setQuery($query);
		$invoiceClient = $db->loadResult();
			
		$db = JFactory::getDBO();
		$query = $db->getQuery(true);
			
		$query->select($this->getState('list.select', 'a.*'));	
		$query->from('#__almh_invoices AS a');
		$query->where('a.InvoiceClient =' .$invoiceClient);
		
		$query->select('c.ClientName AS client');
		$query->join('LEFT', '#__almh_clients AS c ON c.ClientId = a.InvoiceClient');
			
		$query->order($db->getEscaped($this->getState('list.ordering', 'a.InvoiceId')).' '.$db->getEscaped($this->getState('list.direction', 'desc')));
			
		return $query;		
	}
}
?>